<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="库位名" prop="name">
        <el-input v-model="dataForm.name" placeholder="库位名" />
      </el-form-item>
      <el-form-item label="父级ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父级ID" />
      </el-form-item>
      <el-form-item label="类型（仓库、楼层、房间、货架）" prop="type">
        <el-input
          v-model="dataForm.type"
          placeholder="类型（仓库、楼层、房间、货架）"
        />
      </el-form-item>
      <el-form-item label="编号" prop="zipCode">
        <el-input v-model="dataForm.zipCode" placeholder="编号" />
      </el-form-item>
      <el-form-item label="负责人" prop="manager">
        <el-input v-model="dataForm.manager" placeholder="负责人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="dataForm.remarks" placeholder="备注" />
      </el-form-item>
      <el-form-item label="是否使用" prop="isUse">
        <el-input v-model="dataForm.isUse" placeholder="是否使用" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        name: "",
        parentId: "",
        type: "",
        zipCode: "",
        manager: "",
        phone: "",
        address: "",
        remarks: "",
        isUse: ""
      },
      dataRule: {
        name: [{ required: true, message: "库位名不能为空", trigger: "blur" }],
        parentId: [
          { required: true, message: "父级ID不能为空", trigger: "blur" }
        ],
        type: [
          {
            required: true,
            message: "类型（仓库、楼层、房间、货架）不能为空",
            trigger: "blur"
          }
        ],
        zipCode: [{ required: true, message: "编号不能为空", trigger: "blur" }],
        manager: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
        remarks: [{ required: true, message: "备注不能为空", trigger: "blur" }],
        isUse: [
          { required: true, message: "是否使用不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/cms/ccWarehouse/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.ccWarehouse.name;
              this.dataForm.parentId = data.ccWarehouse.parentId;
              this.dataForm.type = data.ccWarehouse.type;
              this.dataForm.zipCode = data.ccWarehouse.zipCode;
              this.dataForm.manager = data.ccWarehouse.manager;
              this.dataForm.phone = data.ccWarehouse.phone;
              this.dataForm.address = data.ccWarehouse.address;
              this.dataForm.remarks = data.ccWarehouse.remarks;
              this.dataForm.isUse = data.ccWarehouse.isUse;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/cms/ccWarehouse/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              name: this.dataForm.name,
              parentId: this.dataForm.parentId,
              type: this.dataForm.type,
              zipCode: this.dataForm.zipCode,
              manager: this.dataForm.manager,
              phone: this.dataForm.phone,
              address: this.dataForm.address,
              remarks: this.dataForm.remarks,
              isUse: this.dataForm.isUse
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
